﻿/***********************************************************************************************
 COPYRIGHT 2008 Vijeth D

 This file is part of Function Approximation NeuronDotNet Sample.
 (Project Website : http://neurondotnet.freehostia.com)

 NeuronDotNet is a free software. You can redistribute it and/or modify it under the terms of
 the GNU General Public License as published by the Free Software Foundation, either version 3
 of the License, or (at your option) any later version.

 NeuronDotNet is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with NeuronDotNet.
 If not, see <http://www.gnu.org/licenses/>.

 ***********************************************************************************************/

using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using NeuronDotNet.Core;
using NeuronDotNet.Core.Backpropagation;
using NeuronDotNet.Core.Initializers;
using ZedGraph;

namespace NeuronDotNet.Samples.FunctionApproximation
{
    public partial class MainForm : Form
    {
        private double learningRate = 0.3d;
        private int neuronCount = 10;
        private int cycles = 10000;
        private BackpropagationNetwork network;
        private List<double> data = new List<double>();
        private MyNetwork mn;

        public MainForm()
        {
            InitializeComponent();
        }

        private void LoadForm(object sender, EventArgs e)
        {
            EnableControls(true);
            txtCycles.Text = cycles.ToString();
            txtLearningRate.Text = learningRate.ToString();
            txtNeuronCount.Text = neuronCount.ToString();
        }

        private void EnableControls(bool enabled)
        {
            btnStart.Enabled = enabled;
            btnStop.Enabled = !enabled;
            txtCycles.Enabled = enabled;
            txtLearningRate.Enabled = enabled;
            txtNeuronCount.Enabled = enabled;
            trainingProgressBar.Value = 0;
        }

        private void Start(object sender, EventArgs e)
        {
            EnableControls(false);

            if (!int.TryParse(txtCycles.Text, out cycles)) { cycles = 10000; }
            if (!double.TryParse(txtLearningRate.Text, out learningRate)) { learningRate = 0.25d; }
            if (!int.TryParse(txtNeuronCount.Text, out neuronCount)) { neuronCount = 10; }

            if (cycles <= 0) { cycles = 10000; }
            if (learningRate < 0 || learningRate > 1) { learningRate = 0.25d; }
            if (neuronCount <= 0) { neuronCount = 10; }

            txtCycles.Text = cycles.ToString();
            txtLearningRate.Text = learningRate.ToString();
            txtNeuronCount.Text = neuronCount.ToString();


            mn = new MyNetwork(learningRate, neuronCount, cycles);

            mn.StartLearning(textBoxDby, textBoxY, Mout, trainingProgressBar);
            EnableControls(true);
        }

        private void StopLearning(object sender, EventArgs e)
        {
            mn.StopLearning(textBoxDby, textBoxY, Mout);
            EnableControls(true);
        }

        private void MainFormClosing(object sender, FormClosingEventArgs e)
        {
            mn.StopLearning(textBoxDby, textBoxY, Mout);
        }

    }
}